博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义MVC HtmlHelpe之分页
阅读量:4590 次
发布时间:2019-06-09

本文共 3046 字,大约阅读时间需要 10 分钟。

还在为mvc中没有一个分页htmlhelper 而烦恼吗

那我们何不自己写一个HtmlHelpe;

*首先命名空间一定是System.Web.Mvc

下面是源码

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Web; 6  7 namespace System.Web.Mvc 8 { 9     public static class MyHtmlHelper10     {11         //主要就是输出分页的超级链接的标签12         //自定义分页Helper扩展13         public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)14         {15             var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;16             pageSize = pageSize <= 0 ? 3 : pageSize;17             var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数18             var output = new StringBuilder();19             if (totalPages > 1)20             {21                 //if (currentPage != 1)22                 {
//处理首页连接23 output.AppendFormat("首页 ", redirectTo, pageSize);24 }25 if (currentPage > 1)26 {
//处理上一页的连接27 output.AppendFormat("上一页 ", redirectTo, currentPage - 1, pageSize);28 }29 else30 {31 // output.Append("上一页");32 }33 34 output.Append(" ");35 int currint = 5;36 for (int i = 0; i <= 10; i++)37 {
//一共最多显示10个页码,前面5个,后面5个38 if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)39 {40 if (currint == i)41 {
//当前页处理42 //output.Append(string.Format("[{0}]", currentPage));43 output.AppendFormat("{3} ", redirectTo, currentPage, pageSize, currentPage);44 }45 else46 {
//一般页处理47 output.AppendFormat("{3} ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);48 }49 }50 output.Append(" ");51 }52 if (currentPage < totalPages)53 {
//处理下一页的链接54 output.AppendFormat("下一页 ", redirectTo, currentPage + 1, pageSize);55 }56 else57 {58 //output.Append("下一页");59 }60 output.Append(" ");61 if (currentPage != totalPages)62 {63 output.AppendFormat("末页 ", redirectTo, totalPages, pageSize);64 }65 output.Append(" ");66 }67 output.AppendFormat("第{0}页 / 共{1}页", currentPage, totalPages);//这个统计加不加都行68 69 return new HtmlString(output.ToString());70 }71 }72 }

  调用

  @Html.ShowPageNavigate(pageIndex,pageSize,totalCount)  ;

 

转载于:https://www.cnblogs.com/suppler/p/7150051.html

你可能感兴趣的文章
linux相关
查看>>
day04
查看>>
javassist初接触
查看>>
开发 APP 最重要的 8 个细节
查看>>
centos7 部署haproxy
查看>>
Ajax
查看>>
目录操作
查看>>
java.util.NoSuchElementException问题定位
查看>>
nginx配置socket连接
查看>>
HideTcpip.c
查看>>
UIResponder的API
查看>>
Crazy Binary String<Map法>
查看>>
SVN使用技巧
查看>>
windows下建立netcore控制台程序,然后传送到centos7下的docker容器里运行
查看>>
c# 选择排序
查看>>
API接口安全设计(转)
查看>>
PAT(甲级)Practice1002--Java版
查看>>
数据特征分析——概述
查看>>
ORACLE的查询语句
查看>>
angular2项目关于主页结构分析
查看>>